<?php
require_once("db_functions.php");
require_once("adrese.class.php");
require_once("template.parser.class.php");
/**
 * API za adrese
 * 
 */
class adrese_template extends adrese{
	/**
	 * objekat adrese
	 *
	 * @var object
	 */
	var $adresa_object;
	/**
	 * HTML kod
	 *
	 * @var string
	 */
	var $template_html;
	/**
	 * Parser objekat
	 *
	 * @var object
	 */
	var $parser;
	/**
	 * Tip prikaza liste pretrage ulice(1=standardno,2=vec ima zatamnjivac)
	 *
	 * @var int
	 */
	var $ul_type=1;
	/**
	 * Generisanje HTML koda za prikaz: ulice i broja, lokacije(grad-lok.opstina-naselje), opstine i regiona za zadatu sifru adrese
	 * @param int ID adrese
	 * 
	 */
	function adrese_template($lid,$ul_type=1){
		$this->ul_type=$ul_type;
		$this->adrese();
		$this->parser = new template_parser();
		if($lid){
			$this->adresa_object = $this->getAll($lid);
		}
	}
	/**
	 * Inicijalizacija imena objekata iz template-a
	 *
	 * @param array naziv/id objekta na strani
	 * 
	 */
	
	function defineFieldNames($names){
		foreach ($names as $name=>$value){
			$this->parser->assign($name,$value);
		}
	}
	/**
	 * Parsovanje zeljenog template-a
	 *
	 * @param string tip generisanja koda(ulica, naselje, lok_opstina, grad, opstina, region)
	 * @param object stil(br,input,label,select)
	 */
	function parseIt($type,$style=array()){
		switch ($type){
			case "new":
				$this->template_html = $this->parser->parseTemplate("adresa/new.tpl");
				break;
			case "ulica":
				$this->parser->assign("t_ul_name",$this->adresa_object["ulica"]);
				$this->parser->assign("t_ul_id",$this->adresa_object["ulica_id"]);
				$this->parser->assign("t_ul_broj",$this->adresa_object["broj"]);
				$this->parser->assign("search_ul_type",$this->ul_type);
				$this->template_html = $this->parser->parseTemplate("adresa/ulica.tpl");
				break;
			case "naselje":
				$this->assignAll("naselje");
				$this->template_html = $this->parser->parseTemplate("adresa/naselje.tpl");
				break;
			case "lok_opstina":
				$this->assignAll("lok_opstina");
				$this->template_html = $this->parser->parseTemplate("adresa/lok_opstina.tpl");
				break;
			case "grad":
				$this->assignAll("grad");
				$this->template_html = $this->parser->parseTemplate("adresa/grad.tpl");
				break;
			case "opstina":
				$this->assignAll("opstina");
				$this->template_html = $this->parser->parseTemplate("adresa/opstina.tpl");
				break;
			case "region":
				$this->assignAll("region");
				$this->template_html = $this->parser->parseTemplate("adresa/region.tpl");
				break;
			default:
				//assigning onchange functions for selects
				$this->parser->assign("onchange_region"," onchange=\"updateSelectsAdresa(this,'region')\"");
				$this->parser->assign("onchange_opstina"," onchange=\"updateSelectsAdresa(this,'opstina')\"");
				$this->parser->assign("onchange_grad"," onchange=\"updateSelectsAdresa(this,'grad')\"");
				$this->parser->assign("onchange_lokopstina"," onchange=\"updateSelectsAdresa(this,'lok_opstina')\"");
				$this->parser->assign("onchange_naselje"," onchange=\"updateSelectsAdresa(this,'naselje')\"");
				
				//ulica i broj
				$this->parser->assign("t_ul_name",$this->adresa_object["ulica"]);
				$this->parser->assign("t_ul_id",$this->adresa_object["ulica_id"]);
				$this->parser->assign("t_ul_broj",$this->adresa_object["broj"]);
				$this->parser->assign("search_ul_type",$this->ul_type);
				//naselje
				$this->assignAll("naselje");
				//lok_opstina
				$this->assignAll("lok_opstina");
				//grad
				$this->assignAll("grad");
				//opstina
				$this->assignAll("opstina");
				//region
				$this->assignAll("region");
				
				//$this->template_html = '<script type="text/javascript" language="javascript" src="js/adrese.js"></script>';
				$this->template_html = "<div>";
				$this->template_html .= $this->parser->parseTemplate("adresa/region.tpl");
				$this->template_html .= "<br ".$style["br"]."/>";
				$this->template_html .= $this->parser->parseTemplate("adresa/opstina.tpl");
				$this->template_html .= "<br ".$style["br"]."/>";
				$this->template_html .= $this->parser->parseTemplate("adresa/grad.tpl");
				$this->template_html .= "<br ".$style["br"]."/>";
				$this->template_html .= $this->parser->parseTemplate("adresa/lok_opstina.tpl");
				$this->template_html .= "<br ".$style["br"]."/>";
				$this->template_html .= $this->parser->parseTemplate("adresa/naselje.tpl");
				$this->template_html .= "<br clear=\"all\"/>";
				$this->template_html .= $this->parser->parseTemplate("adresa/ulica.tpl");
				$this->template_html .= '</div>';
				break;
		}
	}
	/**
	 * Assigning values for 
	 * entire set of records
	 *
	 * @param string type(region, opstina)
	 */
	function assignAll($t){
		switch ($t){
			case "region":
				$svi_regioni = $this->getRegions();
				foreach ($svi_regioni as $region){
					$reg_ids[] = $region["region_id"];
					$reg_names[] = $region["region_name"];
					if($region["region_id"] == $this->adresa_object["region"]["id"]){
						$selected_region[] = " selected";
					}else{
						$selected_region[] = "";
					}
				}
				$this->parser->assign("region_id",$reg_ids);
				$this->parser->assign("region_name",$reg_names);
				$this->parser->assign("selected_region",$selected_region);
				break;
			case "opstina":
				$regid = $this->adresa_object["region"]["id"];
				//$sve_opstine = $this->getOpstine($regid);
				$sve_opstine = $this->getOpstine();
				foreach ($sve_opstine as $opstina){
					$ops_ids[] = $opstina["opstina_id"];
					$ops_names[] = $opstina["opstina_name"];
					$ops_parents[] = $opstina["opstina_region_id"];
					
					// select the opstina
					if($opstina["opstina_id"] == $this->adresa_object["opstina"]["id"]){
						$selected_ops[] = " selected";
					}else{
						$selected_ops[] = "";
					}
					
					// show opstine in region
					if(!$this->adresa_object["region"]["id"] || ($opstina["opstina_region_id"] == $this->adresa_object["region"]["id"])){
						$display_opstine[] = "display:block;";
					}else{
						$display_opstine[] = "display:none;";
					}
					
				}
				$this->parser->assign("opstina_id",$ops_ids);
				$this->parser->assign("opstina_name",$ops_names);
				$this->parser->assign("opstina_parent",$ops_parents);
				$this->parser->assign("selected_opstina",$selected_ops);
				$this->parser->assign("display_opstina",$display_opstine);
				break;
			case "grad":
				//$svi_gradovi = $this->getLokacijeAll($this->adresa_object["opstina"]["id"]);
				$svi_gradovi = $this->getLokacijeAll(1);
				foreach ($svi_gradovi as $grad){
					$grad_ids[] = $grad["lid"];
					$grad_names[] = $grad["lok_naziv"];
					$grad_ptts[] = $grad["lok_ptt"];
					$grad_parents[] = $grad["lok_parent"];
					
					// select the grad
					if($grad["lid"] == $this->adresa_object["lokacija"]["mesto"]["id"]){
						$selected_gr[] = " selected";
					}else{
						$selected_gr[] = "";
					}
					
					// show gradovi in opstina
					if(!$this->adresa_object["opstina"]["id"] || ($grad["lok_parent"] == $this->adresa_object["opstina"]["id"])){
						$display_grad[] = "display:block;";
					}else{
						$display_grad[] = "display:none;";
					}
				}
				$this->parser->assign("grad_id",$grad_ids);
				$this->parser->assign("grad_name",$grad_names);
				$this->parser->assign("grad_ptt",$grad_ptts);
				$this->parser->assign("grad_parent",$grad_parents);
				$this->parser->assign("selected_grad",$selected_gr);
				$this->parser->assign("display_grad",$display_grad);
				break;
			case "lok_opstina":
				//$sve_lokopstine = $this->getLokacijeAll($this->adresa_object["lokacija"]["mesto"]["id"],2);
				$sve_lokopstine = $this->getLokacijeAll(2);
				if($sve_lokopstine){
					foreach ($sve_lokopstine as $lok_opstina){
						$lokopstina_ids[] = $lok_opstina["lid"];
						$lokopstina_names[] = $lok_opstina["lok_naziv"];
						$lokopstina_ptts[] = $lok_opstina["lok_ptt"];
						$lokopstina_parents[] = $lok_opstina["lok_parent"];
						
						// select the lok.opstina
						if($lok_opstina["lid"] == $this->adresa_object["lokacija"]["opstina"]["id"]){
							$selected_lo[] = " selected";
						}else{
							$selected_lo[] = "";
						}
						
						// show lok.opstina in grad
						if(!$this->adresa_object["lokacija"]["mesto"]["id"] || ($lok_opstina["lok_parent"] == $this->adresa_object["lokacija"]["mesto"]["id"])){
							$display_lo[] = "display:block;";
						}else{
							$display_lo[] = "display:none;";
						}
					}
					$this->parser->assign("lokopstina_id",$lokopstina_ids);
					$this->parser->assign("lokopstina_name",$lokopstina_names);
					$this->parser->assign("lokopstina_ptt",$lokopstina_ptts);
					$this->parser->assign("lokopstina_parent",$lokopstina_parents);
					$this->parser->assign("selected_lo",$selected_lo);
					$this->parser->assign("display_lokopstina",$display_lo);
				}
				break;
			case "naselje":
				//$sva_naselja = $this->getLokacijeAll($this->adresa_object["lokacija"]["opstina"]["id"],3);
				$sva_naselja = $this->getLokacijeAll(3);
				if($sva_naselja){
					foreach ($sva_naselja as $naselje){
						$naselje_ids[] = $naselje["lid"];
						$naselje_names[] = $naselje["lok_naziv"];
						$naselje_ptts[] = $naselje["lok_ptt"];
						$naselje_parents[] = $naselje["lok_parent"];
						
						// select the naselje
						if($naselje["lid"] == $this->adresa_object["lokacija"]["naselje"]["id"]){
							$selected_naselje[] = " selected";
						}else{
							$selected_naselje[] = "";
						}
						
						// show naselje in lok.opstina
						if(!$this->adresa_object["lokacija"]["opstina"]["id"] || ($naselje["lok_parent"] == $this->adresa_object["lokacija"]["opstina"]["id"])){
							$display_naselje[] = "display:block;";
						}else{
							$display_naselje[] = "display:none;";
						}
					}
					$this->parser->assign("naselje_id",$naselje_ids);
					$this->parser->assign("naselje_name",$naselje_names);
					$this->parser->assign("naselje_ptt",$naselje_ptts);
					$this->parser->assign("naselje_parent",$naselje_parents);
					$this->parser->assign("selected_naselje",$selected_naselje);
					$this->parser->assign("display_naselje",$display_naselje);
				}
				break;
		}
	}
	function addNew(){
		$this->assignAll("region");
		$this->assignAll("opstina");
		$this->assignAll("grad");
		$this->assignAll("lok_opstina");
		$this->assignAll("naselje");
		
		$this->parseIt("new");
	}
}

?>